﻿using log4net.Layout;
using log4net.Layout.Pattern;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Jimmy.Helper
{
    public class ReflectionLayout : PatternLayout
    {
        public ReflectionLayout()
        {
            this.AddConverter("message", typeof(MessagePatternConverter));
            this.AddConverter("ip", typeof(IPPatternConverter));
        }
    }
    public class IPPatternConverter : PatternLayoutConverter
    {
        protected override void Convert(System.IO.TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
        {
            var logMessage = loggingEvent.MessageObject as LogMessage;
            if(logMessage != null)
                writer.Write(logMessage.IP);
        }
    }
    public class MessagePatternConverter : PatternLayoutConverter
    {
        protected override void Convert(System.IO.TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
        {
            var logMessage = loggingEvent.MessageObject as LogMessage;
            if (logMessage != null)
                writer.Write(logMessage.Message);
            else
                writer.Write(loggingEvent.MessageObject);
        }
    }
}
